#include "CCmdInnerManorBattle.h"
#include "../mainctrl.h"
#include "../cmd/dataprepare.h"
#include "commmsg.h"

CT_REGISTER_ASYNCCMD(ID_MAIN_INNER_MANOR_BATTLE, CCmdInnerManorBattle);

int CCmdInnerManorBattle::Do(void* pData)
{
    if (pData != NULL)
    {
        CT_ERROR(("inner cmd CCmdInnerManorBattle get data"));
        return Done(RET_OK);
    }

    if (pstMainCtrl == NULL)
    {
        CT_ERROR(("mainctrl point is null"));
        return Done(RET_SYSTEM_FAIL);
    }

    if (_stUnit._uiMainGID <= 0 && _auiMapUserList.Size() <= 0)
    {
        return Done(RET_OK);//算是正常的逻辑 不作下发
    }

    _iListIndex = 0;

    return LogicDo(RET_OK);
}

int CCmdInnerManorBattle::LogicDo(int iRet)
{
    if (iRet != RET_OK)
    {
        return Done(iRet);
    }

    int iIndex = 0;

	switch (_stUnit._iSubCmd)
	{
    case CManorBattleMgr::CMD_INNER_MANOR_BATTLE_LEAVE:
        {
            PREPARE_PLAYER_DETAIL_INFO(this, _stUnit._uiMainGID);
            ASSERT_PLAYER_DETAIL_PREPARED(_stUnit._uiMainGID);

            iIndex = pstMainCtrl->m_stPlayerDetailInfoHash.HashFind(
                _stUnit._uiMainGID);
            if (iIndex < 0)
            {
                CT_ERROR(("no detail data user (%u)", _stUnit._uiMainGID));
                return Done(RET_SYSTEM_FAIL);
            }
            CPlayerDetailInfo& stPlayerDetail =
                pstMainCtrl->m_stPlayerDetailInfoHash[iIndex];

            pstMainCtrl->m_stManorBattleMgr.LeaveScene(stPlayerDetail, _stUnit);
            if (pstMainCtrl->m_stManorBattleMgr.GetStatEndTime() <
                    pstMainCtrl->m_iNow && _stUnit._iTargetCamp > 0)
            {
                pstMainCtrl->m_stManorBattleMgr.NotifyTargetList(
                    _stUnit._iTargetCamp);
            }
        }
        break;

    case CManorBattleMgr::CMD_MANOR_BATTLE_SUB_TARGET:
        {
            if (_iListIndex < 0) {_iListIndex = 0;}
            for ( ; _iListIndex < _auiMapUserList.Size(); ++_iListIndex)
            {
                _uiTmpGID = _auiMapUserList[_iListIndex];

                if (!_stUnit._bNotifySelf && _uiTmpGID == _stUnit._uiMainGID)
                {
                    continue;
                }

                PREPARE_PLAYER_DETAIL_INFO(this, _uiTmpGID);
                ASSERT_PLAYER_DETAIL_PREPARED(_uiTmpGID);

                iIndex = pstMainCtrl->m_stPlayerDetailInfoHash.HashFind(
                    _uiTmpGID);
                if (iIndex < 0)
                {
                    CT_ERROR(("no detail data user(%u)", _uiTmpGID));
                    continue;
                }
                CPlayerDetailInfo& stPlayerDetail =
                    pstMainCtrl->m_stPlayerDetailInfoHash[iIndex];

                //创建命令时初始化了部分_stAns 这里不能重置
                _stAns.set_gid(_uiTmpGID);
                pstMainCtrl->m_stManorBattleMgr.GetCommInfo(stPlayerDetail,
                    _stAns);

                pstMainCtrl->NotifyOnlineUser(_uiTmpGID, ID_MAIN_MANOR_BATTLE,
                    _stAns, true);
            }
        }
        break;

    case CManorBattleMgr::CMD_MANOR_BATTLE_SUB_REVIVE: //角色复活
        {
            if (_stUnit._uiMainGID > 0 && !_bHasSetAns)
            {
                PREPARE_PLAYER_DETAIL_INFO(this, _stUnit._uiMainGID);
                ASSERT_PLAYER_DETAIL_PREPARED(_stUnit._uiMainGID);

                iIndex = pstMainCtrl->m_stPlayerDetailInfoHash.HashFind(
                    _stUnit._uiMainGID);
                if (iIndex < 0)
                {
                    CT_ERROR(("no detail data user(%u)", _stUnit._uiMainGID));
                    return Done(RET_SYSTEM_FAIL);
                }
                CPlayerDetailInfo& stPlayerDetail =
                    pstMainCtrl->m_stPlayerDetailInfoHash[iIndex];

                iIndex = pstMainCtrl->FindPlayerCacheData(_stUnit._uiMainGID);
                if (iIndex < 0)
                {
                    CT_ERROR(("no cache data user %u", _stUnit._uiMainGID));
                    return Done(RET_SYSTEM_FAIL);
                }
                CPlayerCacheData& stPlayerCache =
                    pstMainCtrl->m_stPlayerCacheDataHash[iIndex];

                _bHasSetAns = true;
                _stAns.Clear();
                _stAns.set_gid(_stUnit._uiMainGID);
                _stAns.set_sub_type(_stUnit._iSubCmd);
                pstMainCtrl->m_stManorBattleMgr.GetCommInfo(stPlayerDetail,
                    _stAns);
                pstMainCtrl->m_stManorBattleMgr.GetRoleInfo(stPlayerCache,
                    _stAns);

                PBMsgHead stMsgHead;
                stMsgHead.set_version(0);
                stMsgHead.set_time(pstMainCtrl->m_iNow);
                stMsgHead.set_msgact(0);
                stMsgHead.set_msgid(ID_MAIN_MANOR_BATTLE);
                stMsgHead.set_msgret(0);
                stMsgHead.set_msgtype(CSMsgDef::EMT_ANSWER);
                
                pstMainCtrl->m_stEncodeBuf.Clear();
                pstMainCtrl->EncodePBMsg(stMsgHead, pstMainCtrl->m_stEncodeBuf);
                pstMainCtrl->EncodePBMsg(_stAns, pstMainCtrl->m_stEncodeBuf);
            }

            if (_iListIndex < 0) {_iListIndex = 0;}
            for ( ; _iListIndex < _auiMapUserList.Size(); ++_iListIndex)
            {
                if (!_stUnit._bNotifySelf &&
                    _auiMapUserList[_iListIndex] == _stUnit._uiMainGID)
                {
                    continue;
                }
                pstMainCtrl->NotifyOnlineUser(_auiMapUserList[_iListIndex],
                    pstMainCtrl->m_stEncodeBuf);
            }

            pstMainCtrl->FlushSendCache();
        }
        break;

    case CManorBattleMgr::CMD_MANOR_BATTLE_SUB_DEAD: //角色死亡
        {
            if (_stUnit._uiMainGID > 0 && !_bHasSetAns)
            {
                PREPARE_PLAYER_DETAIL_INFO(this, _stUnit._uiMainGID);
                ASSERT_PLAYER_DETAIL_PREPARED(_stUnit._uiMainGID);

                iIndex = pstMainCtrl->m_stPlayerDetailInfoHash.HashFind(
                    _stUnit._uiMainGID);
                if (iIndex < 0)
                {
                    CT_ERROR(("no detail data user(%u)", _stUnit._uiMainGID));
                    return Done(RET_SYSTEM_FAIL);
                }
                CPlayerDetailInfo& stPlayerDetail =
                    pstMainCtrl->m_stPlayerDetailInfoHash[iIndex];

                iIndex = pstMainCtrl->FindPlayerCacheData(_stUnit._uiMainGID);
                if (iIndex < 0)
                {
                    CT_ERROR(("no cache data user %u", _stUnit._uiMainGID));
                    return Done(RET_SYSTEM_FAIL);
                }
                CPlayerCacheData& stPlayerCache =
                    pstMainCtrl->m_stPlayerCacheDataHash[iIndex];

                _bHasSetAns = true;
                _stAns.Clear();
                _stAns.set_gid(_stUnit._uiMainGID);
                _stAns.set_sub_type(_stUnit._iSubCmd);
                pstMainCtrl->m_stManorBattleMgr.GetCommInfo(stPlayerDetail,
                    _stAns);
                pstMainCtrl->m_stManorBattleMgr.GetRoleInfo(stPlayerCache,
                    _stAns);

                PBMsgHead stMsgHead;
                stMsgHead.set_version(0);
                stMsgHead.set_time(pstMainCtrl->m_iNow);
                stMsgHead.set_msgact(0);
                stMsgHead.set_msgid(ID_MAIN_MANOR_BATTLE);
                stMsgHead.set_msgret(0);
                stMsgHead.set_msgtype(CSMsgDef::EMT_ANSWER);
                
                pstMainCtrl->m_stEncodeBuf.Clear();
                pstMainCtrl->EncodePBMsg(stMsgHead, pstMainCtrl->m_stEncodeBuf);
                pstMainCtrl->EncodePBMsg(_stAns, pstMainCtrl->m_stEncodeBuf);
            }

            if (_iListIndex < 0) {_iListIndex = 0;}
            for ( ; _iListIndex < _auiMapUserList.Size(); ++_iListIndex)
            {
                if (!_stUnit._bNotifySelf &&
                    _auiMapUserList[_iListIndex] == _stUnit._uiMainGID)
                {
                    continue;
                }
                pstMainCtrl->NotifyOnlineUser(_auiMapUserList[_iListIndex],
                    pstMainCtrl->m_stEncodeBuf);
            }

            pstMainCtrl->FlushSendCache();
        }
        break;

    case CManorBattleMgr::CMD_MANOR_BATTLE_SUB_SCENE_OVER:
        {
            if (_iListIndex < 0) {_iListIndex = 0;}
            for ( ; _iListIndex < _auiMapUserList.Size(); ++_iListIndex)
            {
                _uiTmpGID = _auiMapUserList[_iListIndex];

                if (!_stUnit._bNotifySelf && _uiTmpGID == _stUnit._uiMainGID)
                {
                    continue;
                }

                PREPARE_PLAYER_DETAIL_INFO(this, _uiTmpGID);
                ASSERT_PLAYER_DETAIL_PREPARED(_uiTmpGID);

                iIndex = pstMainCtrl->m_stPlayerDetailInfoHash.HashFind(
                    _uiTmpGID);
                if (iIndex < 0)
                {
                    CT_ERROR(("no detail data user(%u)", _uiTmpGID));
                    continue;
                }
                CPlayerDetailInfo& stPlayerDetail =
                    pstMainCtrl->m_stPlayerDetailInfoHash[iIndex];

                _stAns.Clear();
                _stAns.set_gid(_uiTmpGID);
                _stAns.set_sub_type(_stUnit._iSubCmd);
                pstMainCtrl->m_stManorBattleMgr.GetCommInfo(stPlayerDetail,
                    _stAns);
                pstMainCtrl->m_stManorBattleMgr.GetMapShow(_stAns);

                pstMainCtrl->NotifyOnlineUser(_uiTmpGID, ID_MAIN_MANOR_BATTLE,
                    _stAns, true);
            }
        }
        break;

    case CManorBattleMgr::CMD_MANOR_BATTLE_SUB_TIME_OVER:
        {
            if (_iListIndex < 0) {_iListIndex = 0;}
            for ( ; _iListIndex < _auiMapUserList.Size(); ++_iListIndex)
            {
                _uiTmpGID = _auiMapUserList[_iListIndex];

                if (!_stUnit._bNotifySelf && _uiTmpGID == _stUnit._uiMainGID)
                {
                    continue;
                }

                PREPARE_PLAYER_DETAIL_INFO(this, _uiTmpGID);
                ASSERT_PLAYER_DETAIL_PREPARED(_uiTmpGID);

                iIndex = pstMainCtrl->m_stPlayerDetailInfoHash.HashFind(
                    _uiTmpGID);
                if (iIndex < 0)
                {
                    CT_ERROR(("no detail data user(%u)", _uiTmpGID));
                    continue;
                }
                CPlayerDetailInfo& stPlayerDetail =
                    pstMainCtrl->m_stPlayerDetailInfoHash[iIndex];

                _stAns.Clear();
                _stAns.set_gid(_uiTmpGID);
                _stAns.set_sub_type(_stUnit._iSubCmd);
                _stAns.set_is_break((_stUnit._bIsBreak ? 1 : 0));

                pstMainCtrl->m_stManorBattleMgr.GetCommInfo(stPlayerDetail,
                    _stAns);

                pstMainCtrl->NotifyOnlineUser(_uiTmpGID, ID_MAIN_MANOR_BATTLE,
                    _stAns, true);
            }
        }
        break;

    case CManorBattleMgr::CMD_MANOR_BATTLE_SUB_MONSTER:
        {
            if (_iListIndex < 0) {_iListIndex = 0;}
            for ( ; _iListIndex < _auiMapUserList.Size(); ++_iListIndex)
            {
                _uiTmpGID = _auiMapUserList[_iListIndex];

                if (!_stUnit._bNotifySelf && _uiTmpGID == _stUnit._uiMainGID)
                {
                    continue;
                }

                PREPARE_PLAYER_DETAIL_INFO(this, _uiTmpGID);
                ASSERT_PLAYER_DETAIL_PREPARED(_uiTmpGID);

                iIndex = pstMainCtrl->m_stPlayerDetailInfoHash.HashFind(
                    _uiTmpGID);
                if (iIndex < 0)
                {
                    CT_ERROR(("no detail data user(%u)", _uiTmpGID));
                    continue;
                }
                CPlayerDetailInfo& stPlayerDetail =
                    pstMainCtrl->m_stPlayerDetailInfoHash[iIndex];

                _stAns.Clear();
                _stAns.set_gid(_uiTmpGID);
                _stAns.set_sub_type(_stUnit._iSubCmd);
                pstMainCtrl->m_stManorBattleMgr.GetCommInfo(stPlayerDetail,
                    _stAns);
                pstMainCtrl->m_stManorBattleMgr.GetMonsterInfo(
                    _stUnit._iStatForMon, _stAns);

                pstMainCtrl->NotifyOnlineUser(_uiTmpGID, ID_MAIN_MANOR_BATTLE,
                    _stAns, true);
            }
        }
        break;

    case CManorBattleMgr::CMD_MANOR_BATTLE_SUB_RANK:
        {
            if (_iListIndex < 0) {_iListIndex = 0;}
            for ( ; _iListIndex < _auiMapUserList.Size(); ++_iListIndex)
            {
                _uiTmpGID = _auiMapUserList[_iListIndex];

                if (!_stUnit._bNotifySelf && _uiTmpGID == _stUnit._uiMainGID)
                {
                    continue;
                }

                PREPARE_PLAYER_DETAIL_INFO(this, _uiTmpGID);
                ASSERT_PLAYER_DETAIL_PREPARED(_uiTmpGID);

                iIndex = pstMainCtrl->m_stPlayerDetailInfoHash.HashFind(
                    _uiTmpGID);
                if (iIndex < 0)
                {
                    CT_ERROR(("no detail data user(%u)", _uiTmpGID));
                    continue;
                }
                CPlayerDetailInfo& stPlayerDetail =
                    pstMainCtrl->m_stPlayerDetailInfoHash[iIndex];

                iIndex = pstMainCtrl->FindPlayerCacheData(_stUnit._uiMainGID);
                if (iIndex < 0)
                {
                    CT_ERROR(("no cache data user %u", _stUnit._uiMainGID));
                    continue;
                }
                CPlayerCacheData& stPlayerCache =
                    pstMainCtrl->m_stPlayerCacheDataHash[iIndex];

                _stAns.Clear();
                _stAns.set_gid(_uiTmpGID);
                _stAns.set_sub_type(_stUnit._iSubCmd);
                pstMainCtrl->m_stManorBattleMgr.GetCommInfo(stPlayerDetail,
                    _stAns);

                pstMainCtrl->m_stManorBattleMgr.GetRank(stPlayerCache, _stAns);

                pstMainCtrl->NotifyOnlineUser(_uiTmpGID, ID_MAIN_MANOR_BATTLE,
                    _stAns, true);
            }
        }
        break;

    case CManorBattleMgr::CMD_MANOR_BATTLE_SUB_SCENE_INFO:
        {
            if (_iListIndex < 0) {_iListIndex = 0;}
            for ( ; _iListIndex < _auiMapUserList.Size(); ++_iListIndex)
            {
                _uiTmpGID = _auiMapUserList[_iListIndex];

                if (!_stUnit._bNotifySelf && _uiTmpGID == _stUnit._uiMainGID)
                {
                    continue;
                }

                PREPARE_PLAYER_DETAIL_INFO(this, _uiTmpGID);
                ASSERT_PLAYER_DETAIL_PREPARED(_uiTmpGID);

                iIndex = pstMainCtrl->m_stPlayerDetailInfoHash.HashFind(
                    _uiTmpGID);
                if (iIndex < 0)
                {
                    CT_ERROR(("no detail data user(%u)", _uiTmpGID));
                    continue;
                }
                CPlayerDetailInfo& stPlayerDetail =
                    pstMainCtrl->m_stPlayerDetailInfoHash[iIndex];

                _stAns.Clear();
                _stAns.set_gid(_uiTmpGID);
                _stAns.set_sub_type(_stUnit._iSubCmd);
                pstMainCtrl->m_stManorBattleMgr.GetCommInfo(stPlayerDetail,
                    _stAns);

                pstMainCtrl->m_stManorBattleMgr.GetSceneInfo(_stAns);

                pstMainCtrl->NotifyOnlineUser(_uiTmpGID, ID_MAIN_MANOR_BATTLE,
                    _stAns, true);
            }
        }
        break;

	default:
        CT_ERROR(("manor battle inner unknow sub cmd(%d)",_stUnit._iSubCmd));
        return Done(ERROR_MAIN_SUB_CMD);
	}

    return Done(RET_OK);
}

